#include "Settings.hh"

using namespace std;

Settings::Settings(){
};
Settings::Settings(const char* filename){
  SetFile( filename );
  ReadSettings();
  if(fVerboseLevel>1)
    PrintSettings();
};
Settings::~Settings(){
  cout << "destructor" << endl;
};
void Settings::ReadSettings(){

  TEnv *channels = new TEnv(fInputFile.data()); 
  fVerboseLevel = channels->GetValue("VerboseLevel",0);
  if(fVerboseLevel>2)
    cout << "reading general stuff" << endl; 
 
  fMinimalStripRatio = channels->GetValue("Minimal.Strip.Ratio",0.0);
  fMinimalStripRearRatio = channels->GetValue("Minimal.StripRear.Ratio",0.0);

  if(fVerboseLevel>2)
    cout << "reading cd" << endl;
  fCDTBLowerThresh = channels->GetValue("CD.TB.LowerThresh",-1);
  fCDTBUpperThresh = channels->GetValue("CD.TB.UpperThresh",-1);
  fCDLRLowerThresh = channels->GetValue("CD.LR.LowerThresh",-1);
  fCDLRUpperThresh = channels->GetValue("CD.LR.UpperThresh",-1);
  for(int half=0;half<2;half++){
    for(int hit=0;hit<2;hit++){
      fCDRingEnergy[half][hit] = channels->GetValue(Form("CD.Ring.Energy.%d.%d", half, hit),-1);
      fCDRingPosition[half][hit] = channels->GetValue(Form("CD.Ring.Position.%d.%d", half, hit),-1);
      fCDStripEnergy[half][hit] = channels->GetValue(Form("CD.Strip.Energy.%d.%d", half, hit),-1);
      fCDStripPosition[half][hit]= channels->GetValue(Form("CD.Strip.Position.%d.%d", half, hit),-1);

    }
    fCDDeltaThreshRing[half]= channels->GetValue(Form("CD.Delta.Thresh.Ring.%d", half),-1);
    fCDDeltaThreshStrip[half]= channels->GetValue(Form("CD.Delta.Thresh.Strip.%d", half),-1);
  }
  for(int i=0;i<4;i++){
    fCDEChan[i] = channels->GetValue(Form("CD.E.Chan.%d", i),-1);
  }

  if(fVerboseLevel>2)
    cout << "reading adcs" << endl;
  for(int i=0; i<4; i++){
    fRearECDModStrip[i] = channels->GetValue(Form("RearECD.ModStrip.%d", i),-1);
    fRearECDModRing[i] = channels->GetValue(Form("RearECD.ModRing.%d", i),-1);
    fRearECDMod[i] = channels->GetValue(Form("RearECD.Mod.%d", i),-1);
	 fFDeltaChan[i] = channels->GetValue(Form("Forward.Delta.Chan.%d", i),-1);
    fBDeltaChan[i] = channels->GetValue(Form("Backward.Delta.Chan.%d", i),-1);
    fFDeltaThresh[i] = channels->GetValue(Form("Forward.Delta.Thresh.%d", i),-1);
    fBDeltaThresh[i] = channels->GetValue(Form("Backward.Delta.Thresh.%d", i),-1);
    fFEChan[i] = channels->GetValue(Form("Forward.E.Chan.%d", i),-1);
    fBEChan[i] = channels->GetValue(Form("Backward.E.Chan.%d", i),-1);
    fFEThresh[i] = channels->GetValue(Form("Forward.E.Thresh.%d", i),-1);
    fBEThresh[i] = channels->GetValue(Form("Backward.E.Thresh.%d", i),-1);
    fFStripMod[i] = channels->GetValue(Form("Forward.Strip.Mod.%d", i),-1);
    fBStripMod[i] = channels->GetValue(Form("Backward.Strip.Mod.%d", i),-1);
    fFFirstStrip[i] = channels->GetValue(Form("Forward.FirstStrip.%d", i),-1);
    fBFirstStrip[i] = channels->GetValue(Form("Backward.FirstStrip.%d", i),-1);
    fFEGainFactor[i] = channels->GetValue(Form("Forward.E.GainFactor.%d", i),1.0);
  }
  if(fVerboseLevel>2)
    cout << "reading dgfs" << endl;
  fNofDgfs = channels->GetValue("Dgf.Nof.Mods",48);
  fNofDgfChans = channels->GetValue("Dgf.Nof.Chans",4);
  fNofAdcs = channels->GetValue("Adc.Nof.Mods",7);
  fNofAdcChans = channels->GetValue("Adc.Nof.Chans",32);
  fBeamdumpDgf = channels->GetValue("Beamdump.Dgf",54);
  for(int i=0; i<9; i++)
    fFirstDgf[i] = channels->GetValue(Form("Dgf.First.%d",i),0);
  fCoreChannel = channels->GetValue("Dgf.Core.Channel",0);
  fDgfThresh = channels->GetValue("Dgf.Threshold",0);

  
};
void Settings::PrintSettings(){
  cout << "Thresholds" << endl;
  cout << "Minimal.Strip.Ratio\t" << fMinimalStripRatio << endl;
  cout << "Minimal.StripRear.Ratio\t" << fMinimalStripRearRatio << endl;
  cout << "CD.TB.LowerThresh\t" << fCDTBLowerThresh << endl;
  cout << "CD.TB.UpperThresh\t" << fCDTBUpperThresh << endl;
  cout << "CD.LR.LowerThresh\t" << fCDLRLowerThresh << endl;
  cout << "CD.LR.UpperThresh\t" << fCDLRUpperThresh << endl;

  for(int half=0;half<2;half++){
    for(int hit=0;hit<2;hit++){
      cout << Form("CD.Ring.Energy.%d.%d\t", half,hit) << fCDRingEnergy[half][hit] << endl;
      cout << Form("CD.Ring.Position.%d.%d\t", half,hit) << fCDRingPosition[half][hit] << endl;
      cout << Form("CD.Strip.Energy.%d.%d\t", half,hit) << fCDStripEnergy[half][hit] << endl;
      cout << Form("CD.Strip.Position.%d.%d\t", half,hit) << fCDStripPosition[half][hit] << endl;
    }
    cout << Form("CD.Delta.Thresh.Ring.%d\t", half) << fCDDeltaThreshRing[half] << endl;
    cout << Form("CD.Delta.Thresh.Strip.%d\t", half) << fCDDeltaThreshStrip[half] << endl;
  }
  for(int i=0; i<4; i++){
    cout << Form("CD.E.Chan.%d\t", i) << fCDEChan[i] << endl;
  }

  for(int i=0; i<4; i++){
    cout << Form("RearECD.ModRing.%d\t", i) << fRearECDModRing[i] << endl;
    cout << Form("RearECD.ModStrip.%d\t", i) << fRearECDModStrip[i] << endl;
    cout << Form("Forward.Delta.Chan.%d\t", i) << fFDeltaChan[i] << endl;
    cout << Form("Backward.Delta.Chan.%d\t", i) << fBDeltaChan[i] << endl;
    cout << Form("Forward.Delta.Thresh.%d\t", i) << fFDeltaThresh[i] << endl;
    cout << Form("Backward.Delta.Thresh.%d\t", i) << fBDeltaThresh[i] << endl;
    cout << Form("Forward.E.Chan.%d\t", i) << fFEChan[i] << endl;
    cout << Form("Backward.E.Chan.%d\t", i) << fBEChan[i] << endl;
    cout << Form("Forward.E.Thresh.%d\t", i) << fFEThresh[i] << endl;
    cout << Form("Backward.E.Thresh.%d\t", i) << fBEThresh[i] << endl;
    cout << Form("Forward.Strip.Mod.%d\t", i) << fFStripMod[i] << endl;
    cout << Form("Backward.Strip.Mod.%d\t", i) << fBStripMod[i] << endl;
    cout << Form("Forward.FirstStrip.%d\t", i) << fFFirstStrip[i] << endl;
    cout << Form("Backward.FirstStrip.%d\t", i) << fBFirstStrip[i] << endl;
    cout << Form("Forward.E.GainFactor.%d\t", i) << fFEGainFactor[i] << endl;
  }
  cout << "Dgf.Nof.Mods\t" << fNofDgfs << endl;
  cout << "Dgf.Nof.Chans\t" << fNofDgfChans << endl;
  cout << "Adc.Nof.Mods\t" << fNofAdcs << endl;
  cout << "Adc.Nof.Chans\t" << fNofAdcChans << endl;
  cout << "Beamdump.Dgf\t" << fBeamdumpDgf << endl;
  for(int i=0; i<9; i++)
    cout << Form("Dgf.First.%d\t",i) << fFirstDgf[i] << endl;
  cout << "Dgf.Core.Channel\t" << fCoreChannel << endl;
  cout << "Dgf.Threshold\t" << fDgfThresh << endl;

};
